class Solution {
public:
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        int old=image[sr][sc];
        queue<pair<int,int>> q;
        int m=image.size();
        int n=image[0].size();
        if(old==color) return image;
        q.push({sr,sc});
        while(q.size())
        {
            auto [x,y]=q.front();
            q.pop();
            image[x][y]=color;
            for(int i=0;i<4;++i)
            {
                int a=x+dx[i];
                int b=y+dy[i];
                if(a>=0&&a<m&&b>=0&&b<n&&image[a][b]==old)
                    q.push({a,b});
            }
        }
        return image;
    }
};
